package com.inool.controller;


import com.inool.common.exception.InoolException;
import com.inool.common.login.LoginUser;
import com.inool.common.login.LoginUserHolder;
import com.inool.common.result.Result;
import com.inool.model.Course;
import com.inool.model.CourseSchedule;
import com.inool.model.dto.CourseScheduleDto;
import com.inool.service.CourseScheduleService;
import com.inool.vo.ConfrimCourseRequestVO;
import com.inool.vo.OpenCourseRequestVO;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;


@Tag(name = "课程管理")
@RestController
@RequestMapping("/api/course-schedules")
public class CourseScheduleController {

    @Autowired
    private CourseScheduleService courseScheduleService;


    /**
     * 开设课程的接口
     * @param requestVO 包含课程 ID 和教师编号的请求体
     * @return 操作结果
     */
    @PostMapping("/open")
    @Operation(summary = "开设课程")
    public Result<String> openCourse(@RequestBody OpenCourseRequestVO requestVO) {
        // 调用服务层方法开设课程
        return courseScheduleService.openCourse(requestVO.getCourseId(), requestVO.getStaffNumber());
    }

    @PostMapping("/confirm")
    @Operation(summary = "确认课程")
    public Result<String> confirmCourse(@RequestBody ConfrimCourseRequestVO requestVO) {
        // 调用服务层方法开设课程
        return courseScheduleService.confirmCourse(requestVO);
    }
    @GetMapping("/select-course/{courseScheduleId}")
    @Operation(summary = "学生选课")
    public Result<String> selectCourse(@PathVariable Long courseScheduleId) {
        // 调用服务层方法学生选课
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        String studentNumber = loginUser.getUserId();
        return Result.ok(courseScheduleService.selectCourse(courseScheduleId, studentNumber));

    }
    @GetMapping("/remove/{courseScheduleId}")
    @Operation(summary = "移除课程安排")
    public Result<Boolean> removeCourseById(@PathVariable Long courseScheduleId) {
        // 调用服务层方法学生选课
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        return Result.ok(courseScheduleService.removeCourse(courseScheduleId));

    }
    
    @PostMapping("add")
    public Result<CourseSchedule> addCourseSchedule(@RequestBody CourseSchedule courseSchedule) {
        CourseSchedule newCourseSchedule = courseScheduleService.addCourseSchedule(courseSchedule);
        return Result.ok(newCourseSchedule);
    }

    @GetMapping("get-selected")
    @Operation(summary = "获取学生的已选课程")
    public Result<List<CourseSchedule>> getSelectedCourses() {
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        if (loginUser == null) {
            throw new InoolException(null, "未登录");
        }

        return Result.ok(courseScheduleService.getSelectedCourses(loginUser.getUserId()));
    }
    
    @GetMapping("/cancel")
    @Operation(summary = "学生取消选课")
    public Result<Boolean> cancelCourse(@RequestParam Long courseScheduleId) {
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        if (loginUser == null) {
            throw new InoolException(null, "未登录");
        }
        return Result.ok(courseScheduleService.cancelCourse(courseScheduleId, loginUser.getUserId()));
    }

    @GetMapping("get-all")
    @Operation(summary = "获取所有开设课程")
    public Result<List<CourseSchedule>> getAllCourseSchedules() {
        List<CourseSchedule> courseSchedules = courseScheduleService.getAllCourseSchedules();
        return Result.ok(courseSchedules);
    }

    
    @Operation(summary = "获取教师的课程")
    @GetMapping("teacher")
    public Result<List<CourseScheduleDto>> getCoursesByToken() {
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        if (loginUser == null) {
            throw new InoolException(null, "未登录");        
        }
        if (!"TEACH".equals(loginUser.getRole())) {
            throw new InoolException(null, "你不是教师");
        }        
        return Result.ok(courseScheduleService.getCoursesByStaffNumber(loginUser.getUserId()));
    }
}    